package org.jeecg.modules.sggg.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.jeecg.modules.sggg.entity.SgMember;
import org.jeecg.modules.sggg.mapper.SgMemberMapper;
import org.jeecg.modules.sggg.service.ISgMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 居民表
 * @Author: jeecg-boot
 * @Date: 2022-10-17
 * @Version: V1.0
 */
@Service("memberServiceImpl")
public class SgMemberServiceImpl extends ServiceImpl<SgMemberMapper, SgMember> implements ISgMemberService {

    @Autowired
    private SgMemberMapper mapper;

    @Autowired
    private ISgMemberService sgMemberService;

    @Override
    public SgMember queryByPhone(String phone) {
        return mapper.queryByPhone(phone);
    }

    @Override
    public List<Map<String, Object>> querydoctorNum(String orgCode) {
        return mapper.querydoctorNum(orgCode);
    }

    @Override
    public List<Map<String, Object>> interveneListByMid(String memberId, Integer type) {
        switch (type) {
            case 0:
                return mapper.blutdruckListByMid(memberId, type);
            case 1:
                return mapper.glucoseListByMid(memberId, type);
            case 2:
                return mapper.interveneListByMid(memberId, type);
            default:
                return null;
        }
    }

    @Override
    public Integer getincreaseCount(String orgCode, String id) {
        return mapper.getincreaseCount(orgCode, id);
    }

    /*
     * 今日新增居民信息
     * */
    @Override
    public List<SgMember> getAddMemberByToday(String orgCode, String roleCode, String doctorId, String date, List<String> responsibleAgencies) {
        return mapper.getAddMemberByToday(orgCode, roleCode, doctorId, date, responsibleAgencies);
    }

    @Override
    public Boolean updateCurrencyById(String memberId, Integer currency) {
        return mapper.updateCurrencyById(memberId, currency);
    }

    @Override
    public IPage<SgMember> memberCurrencyRanking(IPage<SgMember> page, SgMember sgMember, String roleCode, String orgCode, String userId) {
        return mapper.memberCurrencyRanking(page, sgMember.getName(), sgMember.getSex(), sgMember.getPhone(), sgMember.getResponsibleAgencies(), sgMember.getIdCard(), sgMember.getThreeTag(), sgMember.getSysOrgCode(), roleCode, orgCode, userId);
    }

    @Override
    public List<Map<String, Object>> getThreeHighDiseaseTrend(String region) {
        return mapper.getThreeHighDiseaseTrend(region);
    }

    @Override
    public List<Map<String, Object>> getHighRiskWarning(String departCode) {
        return mapper.getHighRiskWarning(departCode);
    }

    @Override
    public Boolean deleteByIds(String ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 云雀居民建档
     *
     * @param orgCode 机构编码
     * @param profile 用户信息
     * @return
     */
    @Override
    public Boolean saveMemberYQ(String orgCode, JSONObject profile) {
        SgMember sgMember = new SgMember();
        sgMember.setSysOrgCode(orgCode);
        sgMember.setName(profile.getString("xm"));
        if (Strings.isNotBlank(profile.getString("brdh"))) {
            sgMember.setPhone(profile.getString("brdh"));
        }
        sgMember.setIdCard(profile.getString("sfzh"));
        if (Strings.isNotBlank(profile.getString("csrq"))) {
            sgMember.setBirthday(DateUtil.parse(profile.getString("csrq")));
        }
        if (Strings.isNotBlank(profile.getString("xb"))) {
            sgMember.setSex(profile.getString("xb").contains("男") ? 0 : profile.getString("xb").contains("女") ? 1 : 2);
        }
        if (Strings.isNotBlank(profile.getString("hjdzmc"))) {
            sgMember.setRegion(profile.getString("hjdzmc"));
        }
        if (Strings.isNotBlank(profile.getString("xzz"))) {
            sgMember.setAddress(profile.getString("xzz"));
        }
        if (Strings.isNotBlank(profile.getString("lxrxm"))) {
            sgMember.setUrgentName(profile.getString("lxrxm"));
        }
        if (Strings.isNotBlank(profile.getString("lxrdh"))) {
            sgMember.setUrgentPhone(profile.getString("lxrdh"));
        }
        return sgMemberService.save(sgMember);
    }
}
